using System;

namespace MathService.DataGenericSources
{
    public abstract class DataSource<T>
    {
        public event EventHandler Updated;

        protected virtual void OnUpdated(EventArgs Args) { Updated.Start(this, Args); }

        private readonly Func<DataHost<T>, T> f_DataExtractor;

        private T f_Value;

        public DataHost<T> Host { get; private set; }

        public T Value { get { return f_Value; } }

        protected DataSource(DataHost<T> Host, Func<DataHost<T>, T> DataExtractor)
        {
            f_DataExtractor = DataExtractor;
            this.Host = Host;
            Host.Updated += OnHostUpdated;
        }

        public T GetValue() { return f_Value = f_DataExtractor(Host); }

        protected virtual void OnHostUpdated(object sender, EventArgs e) { OnUpdated(e); }
    }
}